package com.itlong.cloud.resource;

import com.itlong.cloud.POJO.DTO.common.ParkingMessageDTO;
import com.itlong.cloud.common.network.tcp.netty.INettyServiceClient;
import com.itlong.cloud.constants.RedisConstant;
import com.itlong.cloud.enumerate.ParkingBusinessOrderEnum;
import com.itlong.cloud.network.tcp.netty.ChannelPoolManage;
import com.itlong.cloud.service.INettyService;
import com.itlong.cloud.service.RedisService;
import com.itlong.cloud.utils.node.JsonUtil;
import io.netty.buffer.Unpooled;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * <desc>
 * 暴露操作netty通道接口Feign代理实现类
 * </desc>
 *
 * @createDate 2017/12/07
 */
@RestController
public class INettyServiceResource implements INettyServiceClient {

    private static final Logger LOG = LoggerFactory.getLogger(INettyServiceClient.class);

    @Autowired
    INettyService iNettyService;

    @Autowired
    RedisService redisService;
    /**
     * <desc>
     *      发送查询出入场记录指令
     * </desc>
     *
     * @param clientKey     客户端的唯一标识KEY
     * @param content       参数(JSON格式)
     * @param parkingBusinessOrderEnum      出入场类型
     * @author Wenchao.L
     * @createDate 2018/11/7
     */
    @RequestMapping(path="/network/netty/sendQueryInOrOutRecord",method = {RequestMethod.POST})
    public void sendQueryInOrOutRecord(@RequestParam("clientKey") String clientKey,
                                       @RequestParam("content") String content,
                                       @RequestParam("parkingBusinessOrderEnum") ParkingBusinessOrderEnum parkingBusinessOrderEnum,
                                       @RequestParam("projectId") String projectId) throws IOException {
        try {
            ParkingMessageDTO message = new ParkingMessageDTO(clientKey, content,
                    parkingBusinessOrderEnum.getType());
            String result = JsonUtil.toJSON(message);
            ChannelPoolManage.get(clientKey).writeAndFlush(Unpooled.
                    copiedBuffer((result + "\001").getBytes()));
            redisService.set(RedisConstant.PARKING_IS_ONLINE_KEY+projectId,RedisConstant.PARKING_IS_ONLINE_CACHE_TIME,"1");
        }catch (Exception e){
            //此缓存用于标记项目下停车场在线状态
            redisService.del(RedisConstant.PARKING_IS_ONLINE_KEY+projectId);
        }
    }


    /**
     * <desc>
     *      发送停车场同步指令消息（查询费用，缴纳费用，缴纳月卡）
     * </desc>
     *
     * @param clientKey     客户端的唯一标识KEY
     * @param content       参数(JSON格式)
     * @param parkingBusinessOrderEnum      出入场类型
     * @param messageId     消息唯一ID（传入20位以内唯一ID即可）
     * @author Wenchao.L
     * @createDate 2018/11/7
     */
    @RequestMapping(path = "/network/netty/synOrderByParking", method = {RequestMethod.POST})
    public ParkingMessageDTO synOrderByParking(@RequestParam("clientKey") String clientKey,
                                      @RequestParam("content") String content,
                                      @RequestParam("parkingBusinessOrderEnum") ParkingBusinessOrderEnum parkingBusinessOrderEnum,
                                      @RequestParam("messageId") String messageId) throws IOException, InterruptedException {
        ParkingMessageDTO dto = iNettyService.sendQueryInOrOutRecord(clientKey, content, parkingBusinessOrderEnum, messageId);
        return dto;
    }


}
